<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.youdi365.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author:  心云间、凝听 <981248356@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

use Admin\Builder\AdminFormBuilder;
use Admin\Builder\AdminListBuilder;

class LinkController extends AdminController {
    protected $linkModel;
    protected $link_type;
    function _initialize()
    {
        parent::_initialize();
        $this->linkModel = D('Links');
        $this->link_type=array('1' => '友情链接', '2' => '合作伙伴');
    }

    public function index() {
        // 搜索
        $keyword = I('keyword', '', 'string');
        $condition = array('like','%'.$keyword.'%');
        $map['id|title'] = array($condition, $condition,'_multi'=>true);

        // 获取所有链接
        $paged=I('get.p/d',1);
        $map['status'] = array('egt', '0');  // 禁用和正常状态
        list($data_list,$totalCount) = $this->linkModel->getListByPage($map,$paged,'sort desc,create_time desc','*',20);

        $builder = new AdminListBuilder();
        $builder->setMetaTitle('友情链接')  // 设置页面标题
                ->addTopButton('addnew')    // 添加新增按钮
                ->addTopButton('resume')  // 添加启用按钮
                ->addTopButton('forbid')  // 添加禁用按钮
                ->setSearch('请输入ID/链接标题', U('index'))
                ->keyListItem('title', '标题')
                ->keyListItem('url', '链接','link','#')
                ->keyListItem('image', '图像', 'avatar')
                ->keyListItem('type', '类型', 'callback', array($this->linkModel,'link_type'))
                ->keyListItem('rating','评级')
                ->keyListItem('sort', '排序')
                ->keyListItem('status', '状态', 'status')
                ->keyListItem('right_button', '操作', 'btn')
                ->setListData($data_list)     // 数据列表
                ->setListPage($totalCount,20)  // 数据列表分页
                ->addRightButton('edit')           // 添加编辑按钮
                ->addRightButton('forbid')  // 添加禁用/启用按钮
                ->addRightButton('delete')  // 添加删除按钮
                ->display();
    }

    /**
     * 编辑链接
     */
    public function edit($id=0) {
        $title=$id?"编辑":"新增";
        if (IS_POST) {
            $data = $this->linkModel->create();
            if ($data) {
                $id = $this->linkModel->editData($data);
                if ($id !== false) {
                    $this->success($title.'成功', U('index'));
                } else {
                    $this->error($title.'失败');
                }
            } else {
                $this->error($this->linkModel->getError());
            }
        } else {
            if ($id!=0) {
                $link_data=$this->linkModel->find($id);
            }
            $builder = new AdminFormBuilder();
            $builder->setMetaTitle($title.'友情链接')  // 设置页面标题
                    ->addFormItem('id', 'hidden', 'ID', 'ID')
                    ->addFormItem('title', 'text', '标题', '标题')
                    ->addFormItem('url', 'text', '链接', '点击跳转链接')
                    ->addFormItem('image', 'picture', '图像', '可以是网站Logo')
                    ->addFormItem('target', 'dropdown', '打开方式', '',array('_blank'=>'新的窗口打开','_self'=>'本窗口打开'))
                    ->addFormItem('type', 'radio', '类型', '链接类型',$this->link_type)
                    ->addFormItem('rating', 'number', '级别', '用于评级别')
                    ->addFormItem('sort', 'number', '排序', '用于显示的顺序')
                    ->setFormData($link_data)
                    ->addButton('submit')->addButton('back')    // 设置表单按钮
                    ->display();
        }
    }

}